//
//  HGUtil.m
//  HGMM
//
//  Created by 2008-bj5 on 15/8/4.
//  Copyright (c) 2015年 cc. All rights reserved.
//

#import "HGUtil.h"
#import "MBProgressHUD.h"
//#import "AppUtil.h"
#import "UIView+ViewController.h"
#import "UIViewExt.h"
#import "ZJPickerView.h"
#import "MultiSeleViewController.h"

@interface HGUtil ()



@end


@implementation HGUtil

- (void)toast:(CDVInvokedUrlCommand*)command{
    CCLog(@"HGApp - toast");
    
    if ([command.arguments count] == 1) {
        NSString* myarg = [command.arguments objectAtIndex:0];
        CCLog(@"HGAPP toast: %@", myarg);
        dispatch_async(dispatch_get_main_queue(), ^{
            UIWindow *keywindow = [UIApplication sharedApplication].keyWindow;
            MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:keywindow];
            [HUD setMode:MBProgressHUDModeText];
            [keywindow addSubview:HUD];
            HUD.labelText = myarg;
            [HUD show:YES];
            [HUD hide:YES afterDelay:1.0];
        });
    } else if ([command.arguments count] == 6) {
        
    } else {
        CCLog(@"HGApp - toast: ERROR");
    }
    
}

- (void)picker:(CDVInvokedUrlCommand*)command{
    CCLog(@"HGUtil - picker");
    

    [self _initPickerWithCommand:command];
    
    
    
}

- (void)copy:(CDVInvokedUrlCommand*)command{
    CCLog(@"HGApp - copy");
    
    if ([command.arguments count] == 1) {
        NSString* myarg = [command.arguments objectAtIndex:0];
        CCLog(@"HGAPP copy: %@", myarg);
        
        UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
        pasteboard.string = myarg;
        
        dispatch_async(dispatch_get_main_queue(), ^{
            UIWindow *keywindow = [UIApplication sharedApplication].keyWindow;
            MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:keywindow];
            [HUD setMode:MBProgressHUDModeText];
            [keywindow addSubview:HUD];
            HUD.labelText = @"复制成功";
            [HUD show:YES];
            [HUD hide:YES afterDelay:1.0];
        });
    } else {
        CCLog(@"HGApp - copy: ERROR");
    }
    
}

- (void)paste:(CDVInvokedUrlCommand*)command{
    CCLog(@"HGApp - paste");
    
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    NSString* myarg = pasteboard.string;
    
    if (!myarg) {
        myarg = @"";
    }
    
    CDVPluginResult* pluginResult = nil;
    
    pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:myarg];
    
    [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
    
    
}


-(void)_initPickerWithCommand:(CDVInvokedUrlCommand *)command{
    
    if ([command.arguments count] != 3) {
        
        CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"参数传入错误"];
        [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
        
        return;
        
   }
    
    NSString *title = command.arguments[0];
    BOOL flag = [command.arguments[1] boolValue];
    NSArray *array = command.arguments[2];
    
    if (!flag) //单选
    {
        
        NSDictionary *dict = @{  //ZJPickerViewPropertyTipLabelTextKey
                               @"ZJPickerViewPropertyTipLabelTextKey":title,
                               @"ZJPickerViewPropertySureBtnTitleColorKey":[UIColor blueColor],
                               @"ZJPickerViewPropertyIsShowSelectContentKey":[NSNumber numberWithBool:YES],
                               @"ZJPickerViewPropertyTipLabelTextFontKey":[UIFont systemFontOfSize:18]
                               };
        
        [ZJPickerView zj_showWithDataList:array propertyDict:dict completion:^(NSString * _Nullable selectContent) {
            
           NSInteger index = [array indexOfObject:selectContent];
            
            CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsNSInteger:index];
            
            [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
            
        }];
        
        
    }
    else //多选
    {
        
        
        MultiSeleViewController *milt = [[MultiSeleViewController alloc]init];
        UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:milt];
        milt.multitle = title;
        milt.dateArray = array;
        milt.muilBlock = ^(NSMutableArray *array ){
            
            NSMutableArray *muta = [NSMutableArray array];
            
            for (NSString *string in array) {
                
                NSInteger index = [array indexOfObject:string];
                NSNumber *num = [NSNumber numberWithInteger:index];
                [muta addObject:num];
            }
            
            CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:muta];
            [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
            
        };
        
        [self.webView.viewContoller presentViewController:nav animated:YES completion:^{
            
        }];
        
        
        
    }
    
    
    
    
}



@end
